/***************************************************************************
 *   Copyright (C) 2005 by xihe   *
 *   xihels@163.com                                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ReceiveIMBody.h"
int filterMsg(string& msg, const uint8_t* buf, int len) // 过虑消息
{
    // 消息正文，0是分隔符
    //     先不停的读，直到找到0x0为止
    //     要注意的是Gaim 0.64的QQ插件在这里不以0结尾，对于这种情况，我们需要处理
    //     不然Gaim 0.64发来的消息会解析错误 

    const uint8_t* pos = buf;
    const uint8_t* max = buf + len;

    for (;(pos < max) && (*pos); pos++) NULL;

    msg.assign(buf, pos);
    return pos - buf;
}

NormalIMBody::NormalIMBody() :
    hasFontAttribute(0),
    messageId(0x0000),
    totalFragments(0x00),
    fragmentSequence(0x00),
    replyType(0x00)
{}
NormalIMBody::~NormalIMBody() {}
int NormalIMBody::getBody(const uint8_t* buf, int len) {
    const uint8_t*  cur = buf;
    cur += readBufDw(hasFontAttribute, cur); // 是否有字体属性
    cur += readBufB(totalFragments, cur);    // 分片数
    cur += readBufB(fragmentSequence, cur);  // 分片序号
    cur += readBufW(messageId, cur);	     // 消息id
    cur += readBufB(replyType, cur);	     /* 消息类型，这里的类型表示是
						正常回复还是自动回复之类的信息 */
    // 消息正文
    int headlen = cur - buf;
    int bodylen = filterMsg(message, cur, len - headlen);
    return headlen + bodylen;

}

ClusterIMBody::ClusterIMBody(uint16_t source_) :
    externalId(0),
    clusterId(0),
    sender(0),
    imSequence(0x0000),
    sendTime(0),
    versionId(0),
    contentType(0),
    clusterSource(source_),
    messageId(0x0000),
    clusterType(0x00),
    totalFragments(0x00),
    fragmentSequence(0x00)
{}

ClusterIMBody::~ClusterIMBody() {}


int ClusterIMBody::getBody(const uint8_t* buf, int len) {
    const uint8_t* cur = buf;

    cur += readBufDw(externalId, cur);	// 群外部ID或者父群ID
    cur += readBufB(clusterType, cur);	// 群类型，1字节

    // 临时群内部ID
    if (QQ_RECV_IM_TEMP_CLUSTER_IM == clusterSource) {
	cur += readBufDw(clusterId, cur);
    }

    cur += readBufDw(sender, cur);	// 发送者
    cur += 2;					// 未知的两字节
    cur += readBufW(imSequence, cur);	// 消息序号

    cur += readBufDw(sendTime, cur);	// 发送时间
    // sendTime *= 1000;

    cur += readBufDw(versionId, cur);	// Member Version ID
    cur += 2;					// 后面的内容长度(不要）

    // 一些扩展信息
    if (QQ_RECV_IM_UNKNOWN_CLUSTER_IM != clusterSource) {
	cur += readBufW(contentType, cur); 	// 有没有自定义表情
	cur += readBufB(totalFragments, cur);	// 分片数
	cur += readBufB(fragmentSequence, cur);	// 分片序号
	cur += readBufW(messageId, cur);	// 消息ID
	cur += 4;					// 4字节未知
    }

    // 消息正文
    int headlen = cur - buf;
    int bodylen = filterMsg(message, cur, len - headlen);
    return headlen + bodylen;
}
